function showTime(t) {
  var d = new Date(t);
  var year = d.getFullYear();
  var month = d.getMonth() + 1; // 0~11
  var date = d.getDate();
  // var week = d.getDay();//0~6
  // week = numOfChinese(week)
  var hour = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
  var min = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
  var sec = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
  var str =
    year +
    "-" +
    (month < 10 ? "0" + month : month) +
    "-" +
    (date < 10 ? "0" + date : date) +
    " " +
    hour +
    ":" +
    min +
    ":" +
    sec;
  return str;
}
// 生成id
function uuids() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4";
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  s[8] = s[13] = s[18] = s[23] = "-";

  var uuids = s.join("");
  return uuids;
}

export const getFormDate = (format) => {
  const date = new Date();
  if (format == "yyyy-MM-dd") {
    return `${date.getFullYear()}-${numberHandle((date.getMonth() + 1))}-${numberHandle(date.getDate())}`;
  } else {
    return `${date.getFullYear()}-${numberHandle((date.getMonth() + 1))}-${numberHandle(date.getDate())} ${numberHandle(date.getHours())}:${numberHandle(date.getMinutes())}:${numberHandle(date.getSeconds())}`;
  }
};

export const getQueryString = (name, locationWin) => {
  const locationWinStr = locationWin.href.slice(locationWin.href.indexOf("?"), locationWin.href.length);
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = locationWinStr.substr(1).match(reg);
  if (r != null) {
    return decodeURI(r[2]);
  }
  return "";
};
export const numberHandle = (num) => {
  return num > 9 ? num : ("0" + num);
};

export const imageCompress = (src, type, maxWidth, maxHeight) => { // 图片转成base64
  return new Promise((resolve, reject) => {
    const canvasDom = document.createElement("canvas");
    const contexts = canvasDom.getContext("2d");
    const img = new Image();
    img.src = src;
    img.crossOrigin = "";
    img.onload = function () {
      const originWidth = this.width; // 原始宽度
      const originHeight = this.height; // 原始高度
      const targetWidth = originWidth;
      const targetHeight = originHeight;
      canvasDom.width = targetWidth;
      canvasDom.height = targetHeight;
      contexts.clearRect(0, 0, targetWidth, targetHeight);
      contexts.drawImage(img, 0, 0, targetWidth, targetHeight);
      const finalBase = canvasDom.toDataURL("image/jpeg", 1);
      resolve(finalBase);
    };
  });
};
/*
    * @param  list 数据列表
    * @param  id 后端返回的id
    **/
export const getParentsById = (list, id) => {
  for (const i in list) {
    if (list[i].id == id) {
      // 查询到就返回该数组对象的id
      return [list[i].id];
    }
    if (list[i].children) {
      const node = getParentsById(list[i].children, id);
      if (node !== undefined) {
        // 查询到把父节把父节点加到数组前面
        node.unshift(list[i].id);
        return node;
      }
    }
  }
};
export const sortBy = (field) => {
  // 根据传过来的字段进行排序
  return (x, y) => {
    return x[field] - y[field];
  };
};
// 阿拉伯数字转成中文数字
export const toChineseNumber = (n) => {
  if (!Number.isInteger(n) && n < 0) {
    throw Error("请输入自然数");
  }
  const digits = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  const positions = ["", "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿", "百亿", "千亿"];
  const charArray = String(n).split("");
  let result = "";
  let prevIsZero = false;
  // 处理0  deal zero
  for (let i = 0; i < charArray.length; i++) {
    const ch = charArray[i];
    if (ch !== "0" && !prevIsZero) {
      result += digits[parseInt(ch)] + positions[charArray.length - i - 1];
    } else if (ch === "0") {
      prevIsZero = true;
    } else if (ch !== "0" && prevIsZero) {
      result += "零" + digits[parseInt(ch)] + positions[charArray.length - i - 1];
    }
  }
  // 处理十 deal ten
  if (n < 100) {
    result = result.replace("一十", "十");
  }
  return result;
};

export { showTime, uuids };
